package quizman.tdg;
/**
 * 
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.dsrg.ts.threadLocal.DbRegistry;

/**
 * @author Team 0i
 *
 */
public class QuestionTDG {
	public static ResultSet find(String quizName) throws Exception {
		try {
			if(quizName==null || quizName.equals(""))
				throw new IllegalArgumentException("Invalid argument to QuestionTDG.find.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("SELECT * from Question where Quiz_Name LIKE ?");
			dbStatement.setString(1, quizName);
			ResultSet rs = dbStatement.executeQuery();
			return rs;
		} catch (Exception e) {
			String msg = "Error in QuestionTDG.find";
			throw new Exception(msg, e);
		}
	}
	public static ResultSet find(String quizName, String questionName) throws Exception {
		try {
			if(quizName==null || quizName.equals(""))
				throw new IllegalArgumentException("Invalid argument to QuestionTDG.find.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("SELECT * from Question where Quiz_Name LIKE ? AND Question_Name LIKE ?");
			dbStatement.setString(1, quizName);
			dbStatement.setString(2, questionName);
			ResultSet rs = dbStatement.executeQuery();
			return rs;
		} catch (Exception e) {
			String msg = "Error in QuestionTDG.find";
			throw new Exception(msg, e);
		}
	}
	
	public static void insert(String questionName, String questionText, String quizName, int questionType) throws Exception {
		try {
			if(questionName==null || questionName.equals(""))
				throw new IllegalArgumentException("Invalid argument to QuestionTDG.insert.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("INSERT INTO Question (Question_Name, Question_Text, Quiz_Name, Question_Type) values(?, ?, ?, ?)");
			dbStatement.setString(1, questionName);
			dbStatement.setString(2, questionText);
			dbStatement.setString(3, quizName);
			dbStatement.setInt(4, questionType);
			dbStatement.executeUpdate();
		} catch (Exception e) {
			String msg = "Error in QuestionTDG.insert";
			throw new Exception(msg, e);
		}
	}
	
	public static void update(String questionName, String questionText) throws Exception {
		try {
			if(questionName==null || questionName.equals(""))
				throw new IllegalArgumentException("Invalid argument to QuestionTDG.update.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("UPDATE Question SET Question_Text = ? WHERE Question_Name LIKE ?");
			dbStatement.setString(1, questionText);
			dbStatement.setString(2, questionName);
			dbStatement.executeUpdate();
		} catch (Exception e) {
			String msg = "Error in QuestionTDG.update";
			throw new Exception(msg, e);
		}
	}
	
	public static void delete(String questionName) throws Exception {
		try {
			if(questionName==null || questionName.equals(""))
				throw new IllegalArgumentException("Invalid argument to QuestionTDG.delete.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("DELETE FROM Question WHERE Question_Name LIKE ?");
			dbStatement.setString(1, questionName);
			dbStatement.executeUpdate();
		} catch (Exception e) {
			String msg = "Error in QuestionTDG.delete";
			throw new Exception(msg, e);
		}
	}
	
	
}